package com.renren.youyou.views;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.renren.youyou.R;
import com.renren.youyou.home.HomeCallback;
import com.renren.youyou.home.HomeCallbackImpl;
import com.renren.youyou.home.HomeConfig;

public class HomeFragment extends BaseFragment<Integer> implements
		MenuLayout.OnMenuItemClickListener,
		IHorizontalFlip.OnMenuOpenAnimationListener,
		IHorizontalFlip.OnRightAnimationListener,
		IHorizontalFlip.OnMenuCloseAnimationListener {
	private static final String TAG = "HomeFragment";
	private int mCurrent = 0;
	private ContentLayout mContainer;
	private HomeLayout mHome;
	private LinearLayout mSideContainer;
	private final MenuTab[] mTabs;
	private final View[] mTabViews;
	private View mSide;
	private final HomeCallback mCallback;
	private boolean mLoading;
	private SideDataAsyncTask mSideDataAsyncTask;

	public HomeFragment() {
		mCallback = new HomeCallbackImpl();
		mTabs = new MenuTab[HomeConfig.TABS.length];
		mTabViews = new View[HomeConfig.TABS.length];

		for (int i = 0; i < HomeConfig.TABS.length; i++) {
			try {
				mTabs[i] = (MenuTab) HomeConfig.TABS[i].newInstance();
			} catch (java.lang.InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onLeftAnimationStart() {
		mHome.onLeftAnimationStart();
		mCallback.onAnimateLeftStart();
	}

	@Override
	public void onLeftAnimationEnd() {
		mCallback.onAnimateLeftEnd();
	}

	@Override
	public void onMenuClick(int position, View v) {
		if (mLoading) {
			return;
		}


		if (mCurrent == position) {
			mContainer.animateOpen();
		} else {
			invisibleCurrentTab();
			mCurrent = position;
			reload();
		}
	}

	@Override
	public void onOpenAnimationStart() {
		mCallback.onAnimateOpenStart();
	}

	@Override
	public void onOpenAnimationEnd() {
		Log.d(TAG, "@onOpenAnimationEnd");
		mCallback.onAnimateOpenEnd();
	}

	@Override
	public void onRightAnimationStart() {
		mHome.onRightAnimationStart();
		mCallback.onAnimateRightStart();
	}

	@Override
	public void onRightAnimationEnd() {
		mCallback.onAnimateRightEnd();
	}

	private void invisibleCurrentTab() {
		mTabViews[mCurrent].setVisibility(View.INVISIBLE);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mHome = new HomeLayout(getActivity());
		mContainer = (ContentLayout) mHome.findViewById(R.id.container);
		mContainer.addMenuCloseAnimationListener(this);
		mContainer.setMenuOpenAnimationListener(this);

		mSideContainer = (LinearLayout) mHome.findViewById(R.id.side);

		return mHome;
	}

	@Override
	protected void onDestroyData() {
		for (View view : mTabViews) {
			mContainer.removeView(view);
		}

		mCallback.onDestroyData();
		mSideContainer.removeView(mSide);
	}

	@Override
	protected void onFinishLoad(Integer integer) {
		Log.d(TAG, "@onFinishLoad");
		// getTitleBar().setTitle(getString(mTabs[mCurrent].getNameResourceId()));
		if (mContainer.getState() == IHorizontalFlip.STATE_MIDDLE) {
			mTabs[mCurrent].onFinishLoad();
			mTabViews[mCurrent].setVisibility(View.VISIBLE);
			mLoading = false;
		} else {
			mContainer.animateOpen();
			mContainer
					.addMenuCloseAnimationListener(new IHorizontalFlip.OnMenuCloseAnimationListener() {
						@Override
						public void onOpenAnimationStart() {
						}

						@Override
						public void onOpenAnimationEnd() {
							mTabs[mCurrent].onFinishLoad();
							mTabViews[mCurrent].setVisibility(View.VISIBLE);
							mLoading = false;
							mContainer.removeMenuCloseAnimationListener(this);
						}
					});
		}
	}

	@Override
	protected Integer onLoadInBackground() {
		Log.d(TAG, "@onLoadInBackground");
		mTabs[mCurrent].onLoadData();

		return mCurrent;
	}

	@Override
	protected void onPreLoad() {
		mLoading = true;

		if (mTabViews[mCurrent] == null) {
			mTabViews[mCurrent] = mTabs[mCurrent].onCreateView(getActivity());
			mContainer.addView(mTabViews[mCurrent], 0,
					new FrameLayout.LayoutParams(
							ViewGroup.LayoutParams.FILL_PARENT,
							ViewGroup.LayoutParams.FILL_PARENT));
		}
	}

	@Override
	public void onResume() {
		super.onResume();

		if (mSideDataAsyncTask == null) {
			mSideDataAsyncTask = new SideDataAsyncTask();
			mSideDataAsyncTask.execute((Void[]) null);
		}
	}

	@Override
	public void onPause() {
		super.onPause();

		for (MenuTab tab : mTabs) {
			tab.onPause();
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		for (MenuTab tab : mTabs) {
			final OnActivityResultListener listener = tab.onActivityResult();

			if (listener != null) {
				Log.d(TAG, "@onActivityResult");
				listener.onActivityResult(requestCode, resultCode, data);
			}
		}
	}

	public static interface OnActivityResultListener {
		void onActivityResult(int requestCode, int resultCode, Intent data);
	}

	private class SideDataAsyncTask extends AsyncTask<Void, Void, Void> {
		@Override
		protected void onPreExecute() {
			if (mSide == null) {
				mSide = mCallback.onCreateSideTab(getActivity());
				mSideContainer.addView(mSide, new FrameLayout.LayoutParams(
						ViewGroup.LayoutParams.FILL_PARENT,
						ViewGroup.LayoutParams.FILL_PARENT));
			}
		}

		@Override
		protected Void doInBackground(Void... params) {
			mCallback.onLoadSideData();

			return null;
		}

		@Override
		protected void onPostExecute(Void aVoid) {
			mCallback.onFinishSideData(mSide);
		}
	}
}
